import {createStore} from "vuex";
import axios from "@/plugin/axios";

export default createStore({
    state: {
        user: {},
        allRoute: [], // 用户所有路由权限
        menuList: [], // 左侧菜单渲染数据
        loadingShow: false,
        activeTabIndex: "",
        pageTitle: '',
        areaOption: [],           // 区域下拉框数据
        stationmasterOption: [],  // 站长下拉框数据
        menuData: [
            {
                title: "主页",
                icon: "menu",
                order: "010",
                children: [
                    {
                        path: "/jxtc/home",
                        url: '/index',
                        menuName: "概览",
                    },
                ],
            },

            {
                title: "站点",
                icon: "Connection",
                order: "020",
                children: [
                    {
                        path: "/jxtc/site",
                        menuName: "站点管理",
                    },
                    {
                        path: "/jxtc/stationmaster",
                        menuName: "站长管理",
                    },
                ],
            },

            {
                title: "店铺&菜品",
                icon: "Dish",
                order: "030",
                children: [
                    {
                        path: "/jxtc/store",
                        menuName: "店铺管理",
                    },
                    {
                        path: "/jxtc/food",
                        menuName: "菜品管理",
                    },
                ],
            },

            {
                title: "营销",
                icon: "MilkTea",
                order: "040",
                children: [
                    {
                        path: "/jxtc/coupon",
                        menuName: "优惠券",
                    },
                ],
            },
            {
                title: "订单管理",
                icon: "Tickets",
                order: "050",
                children: [
                    {
                        path: "/jxtc/orderList",
                        menuName: "订单列表",
                    },
                    {
                        path: "/jxtc/foodList",
                        menuName: "菜品份数表",
                    },
                    {
                        path: '/jxtc/salesOrderList',
                        menuName: "售后订单",
                    }
                ],
            },
            {
                title: "财务管理",
                icon: "Collection",
                order: "060",
                children: [
                    {
                        path: "/jxtc/revenue",
                        menuName: "平台总营收",
                    },
                    {
                        path: '/jxtc/revenueStore',
                        menuName: '店铺营收'
                    },
                    {
                        path: '/jxtc/revenueWages',
                        menuName: '站长及配送工资'
                    }
                ],
            },

        ],
    },
    getters: {
        userData(state) {
            return state.user;
        },
    },
    mutations: {
        setUser(state, value) {
            state.user = value;
        },
        setActiveTabIndex(state, value) {
            state.activeTabIndex = value;
        },

    },
    actions: {
        // 区域
        getAreaOptionData(store, param = {forceUpdate: false, okCallback: null}) {
            let {forceUpdate, okCallback} = param;
            if (!store.state.areaOption.length || forceUpdate) {
                axios.get('/jx/jxRegionManage/queryRegions').then(res => {
                    if (res.code == 200) {
                        res.result.forEach(val => {
                            val.name = val.regionName
                        })
                        store.state.areaOption = res.result;
                        okCallback && okCallback(res.result);
                    }
                })
            } else {
                okCallback && okCallback(store.state.areaOption);
            }
        },
        // 站长
        getStationmasterOptionData(store, forceUpdate = false) {
            if (!store.state.stationmasterOption.length || forceUpdate) {
                let smData = {keyword: ''}
                axios.get('/jx/jxSiteMasterManage/querySiteMaster', {params: smData}).then(res => {
                    if (res.code == 200) {
                        store.state.stationmasterOption = res.result
                    }
                })
            }
        },

        // 获取用户详情/判断用户登录状态
        getMenuList: (store, routeList) => {

            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    let allRoute = [];
                    let menuList = [];
                    let menuDataList = JSON.parse(JSON.stringify(store.state.menuData)); // 不改变原始数据，防止不同权限的登录异常

                    // 匹配url存在的正式存为路由与菜单
                    menuDataList.forEach(item => {
                        let newChildren = []
                        if (item.children) {
                            item.children.forEach(child => {
                                newChildren.push(child)
                                allRoute.push(child.path);
                            })
                        }
                        item.children = newChildren;
                        if (newChildren.length > 0) {
                            menuList.push(item)
                        }
                    })
                    store.state.allRoute = allRoute;
                    store.state.menuList = menuList;
                    resolve(1);

                }, 50);
            });
        },

        // 获取用户详情/判断用户登录状态
        getMenuList222: (store, routeList) => {

            return new Promise((resolve, reject) => {
                getMenuList().then(res => {

                    let allRoute = [];
                    let menuList = [];
                    let menuDataList = JSON.parse(JSON.stringify(store.state.menuData)); // 不改变原始数据，防止不同权限的登录异常

                    let allUrl = []
                    if (res.code == 200) {
                        let arr = res.data || [];
                        // arr[1].menuList = [arr[1].menuList[1]] // 测试

                        // 提取所有url, 用于与child中的匹配
                        arr.forEach(val => {
                            if (val.menuList instanceof Array) {
                                val.menuList.forEach(menu => {
                                    allUrl.push(menu.url)
                                })
                            }
                        })

                        // 匹配url存在的正式存为路由与菜单
                        menuDataList.forEach(item => {
                            let newChildren = []
                            if (item.children) {
                                item.children.forEach(child => {
                                    if (allUrl.includes(child.url)) {
                                        newChildren.push(child)
                                        allRoute.push(child.path);
                                    }
                                })
                            }
                            item.children = newChildren;
                            if (newChildren.length > 0) {
                                menuList.push(item)
                            }
                        })
                        store.state.allRoute = allRoute;
                        store.state.menuList = menuList;

                        console.log('getmuen 完成');
                        resolve(1);
                    } else {
                        resolve(0);
                    }

                }).catch((err) => {
                    if (err.message && err.message.includes("timeout")) {
                        alert("连接超时，可能网络异常");
                    }
                    reject();
                });

            });
        },

    },
    modules: {},
});
